﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows.Forms;

namespace BGShell.Controls
{
    [DefaultProperty("ZoomFactor")]
    [DefaultEvent("ZoomFactorChanged")]
    [System.ComponentModel.DesignerCategory("Code")]
    internal class ZoomContextMenu : ContextMenuStrip
    {
        private const float MaxZoomFactor = 10;

        private static readonly float[] DefaultZoomFactors = new float[]
        {
            4.0f, 2.0f, 1.5f, 1.25f, 1.0f, 0.75f, 0.5f
        };

        private static readonly object EventZoomFactorChanged = new object();

        private float _delta = 0.1f;
        private float _current = 1.0f;
        private float[] _factors = DefaultZoomFactors;

        private ToolStripMenuItem _itemZoomIn;
        private ToolStripMenuItem _itemZoomOut;
        private ToolStripMenuItem _itemCustom;
        private ToolStripMenuItem[] _itemsFactor;

        public ZoomContextMenu()
        {
            CreateItems();
            DefaultDropDownDirection = ToolStripDropDownDirection.AboveRight;
        }

        [DefaultValue(1.0f)]
        public float ZoomFactor
        {
            get { return _current; }
            set 
            {
                if (_current != value && value > 0 && value < MaxZoomFactor)
                {
                    _current = value;
                    OnZoomFactorChanged(EventArgs.Empty);
                }
            }
        }

        public event EventHandler ZoomFactorChanged
        {
            add { Events.AddHandler(EventZoomFactorChanged, value); }
            remove { Events.RemoveHandler(EventZoomFactorChanged, value); }
        }

        [DefaultValue(ToolStripDropDownDirection.AboveRight)]
        public override ToolStripDropDownDirection DefaultDropDownDirection
        {
            get { return base.DefaultDropDownDirection; }
            set { base.DefaultDropDownDirection = value; }
        }

        protected virtual void OnZoomFactorChanged(EventArgs e)
        {
            var handler = Events[EventZoomFactorChanged] as EventHandler;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions")]
        protected override void OnItemClicked(ToolStripItemClickedEventArgs e)
        {
            base.OnItemClicked(e);

            if (e.ClickedItem == _itemZoomIn)
            {
                ZoomFactor += _delta;
            }

            if (e.ClickedItem == _itemZoomOut)
            {
                ZoomFactor -= _delta;
            }

            if (e.ClickedItem == _itemCustom)
            {
                MessageBox.Show("TODO");
            }

            int i = Array.IndexOf(_itemsFactor, e.ClickedItem);

            if (i > -1)
            {
                ZoomFactor = _factors[i];
            }
        }

        protected override void OnOpening(CancelEventArgs e)
        {
            for (int i = 0; i < _factors.Length; i++)
            {
                _itemsFactor[i].Checked = (_factors[i] == _current);
            }

            base.OnOpening(e);
        }

        private void CreateItems()
        {
            Items.Clear();

            _itemZoomIn = CreateItem(Resources.ZoomIn);
            _itemZoomIn.ShortcutKeyDisplayString = Resources.ZoomInShortcut;

            _itemZoomOut = CreateItem(Resources.ZoomOut);
            _itemZoomOut.ShortcutKeyDisplayString = Resources.ZoomOutShortcut;

            _itemsFactor = new ToolStripMenuItem[_factors.Length];
            
            for (int i = 0; i < _factors.Length; i++)
            {
                _itemsFactor[i] = CreateItem(_factors[i].ToString("P0", CultureInfo.CurrentCulture));
                _itemsFactor[i].Tag = _factors[i];
            }

            _itemCustom = CreateItem(Resources.ZoomCustom);

            Items.Add(_itemZoomIn);
            Items.Add(_itemZoomOut);
            Items.Add(new ToolStripSeparator());
            Items.AddRange(_itemsFactor);
            Items.Add(new ToolStripSeparator());
            Items.Add(_itemCustom);
        }

        private static ToolStripMenuItem CreateItem(string text)
        {
            return new ToolStripMenuItem(text);
        }
    }
}
